package com.hjp.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.concurrent.*;

/**
 * @Description: 使用 ThreadFactoryBuilder 来创建线程
 * @Author: huangjp6
 * @Date: 2022/3/17 10:07
 */
public class CreateThread {

    private static final Log logger = LogFactory.getLog(CreateThread.class);

    private static ThreadFactory nameFactory = new ThreadFactoryBuilder()
            .setNameFormat("demo-pool-%d").build();

    private static ExecutorService pool = new ThreadPoolExecutor(5, 100,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(1024), nameFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            pool.execute(new SubThread(i));
        }
        pool.shutdown();
    }

    private static class SubThread implements Runnable {
        private int i = -1;

        public SubThread(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            logger.info(Thread.currentThread().getState() + "----------" + Thread.currentThread().getName() + "----------value-" + i);
        }
    }
}